// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables, non_constant_identifier_names

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:junyeyishu/ui/bulletin/widgets/QGBulletinItem.dart';
import 'package:junyeyishu/ui/sale/widgets/QGSwitchWidget.dart';

import 'package:junyeyishu/utils/utils.dart';
import 'package:junyeyishu/widgets/QGWidget.dart';
import 'package:junyeyishu/widgets/QGCacheListState.dart';
import 'package:junyeyishu/widgets/QGUserNavigationBar.dart';

class QGBulletinPage extends StatefulWidget {
  const QGBulletinPage({super.key});
  @override
  State<QGBulletinPage> createState() => QGBulletinPageState();
}

class QGBulletinPageState extends QGCacheListState<QGBulletinPage> {
  List<String> names = [];

  int type = 0;

  StreamSubscription? subs;

  @override
  dispose() {
    subs?.cancel();
    super.dispose();
  }

  @override
  listParam() {
    if (type == 0) {
      return {};
    }

    if (tab is List && tab.length >= type) {
      return {"category_id": tab[type - 1]?["id"]};
    }

    return super.listParam();
  }

  @override
  void initState() {
    super.initState();

      // 切换到公告分类，刷新
    subs = eventBus.on<EventMainTabRefresh>().listen((event) {
      if (event.index != 2) return;
      onRefresh();
    });

    tabURL = url_anno_categ;
    listURL = url_anno;

    // 
        loadTab();

  }

  @override
  onUpdateTab(data) {
    names = ["全部公告"];
    for (var item in data) {
      names.add(item["name"] ?? "");
    }

    setState(() {});
    onRefresh();
  }

  // 公告分类名字
  getCateName(data) {
    for (var item in tab) {
      if (data["category_id"] == item["id"]) {
        return item["name"];
      }
    }
    return "";
  }

  @override
  Widget itemBuilder(context, index, data) {
    return GestureDetector(
      onTap: () {
        toDetail(index);
      },
      child: QGBulletinItem(data: data, cate: getCateName(data)),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Stack(
          children: [
            QGImage(
                asset: "bulletin_bg",
                width: kScreenWidth,
                height: 22.rpx + kNavHeight),
            Column(
              children: [
                 buildTop(),

                buildSearchBar(),
               SizedBox(height: 25,),

                buildSwitchBar(),
                SizedBox(height: 15.rpx),
                buildList(),
              ],
            ),
          ],
        ));
  }

  buildTop() {
    return Container(
      child: QGUserNavigationBar(title: ""),
    );
  }

  buildSearchBar() {
    return Row(
      children: [
        SizedBox(width: 15,),
        Text('公告',style: TextStyle(fontSize: 20,color: Color(0XFFFFFFFF))),
        SizedBox(width: 10,),

        Expanded(child: 
         GestureDetector(
      onTap: onSearch,
      child: Container(
        // margin: EdgeInsets.only(
        //     bottom: 10.rpx, top: 0.rpx, left: 10.rpx, right: 10.rpx),
        child: Container(
          height: 32.rpx,
          decoration: BoxDecoration(
            color: Colors.transparent,
            borderRadius: BorderRadius.circular(6.rpx),
            border: Border.all(width: 1.rpx, color: Color(0xffFFFFFF)),
          ),
          child: Row(children: [
            SizedBox(width: 10.rpx),
            QGImage(asset: "sale_search",width: 13.rpx),
            SizedBox(width: 6.rpx),
            Text(
              '输入关键字',
              style: TextStyle(
                  color: Color(0xffFFFFFF),
                  fontSize: 12.rpx,
                  fontWeight: FontWeight.normal),
            ),
          ]),
        ),
      ),
    )
        ),
        SizedBox(width: 15,),
   
      ],
    )
    
    ;
  }

  // 切换公告类别
  buildSwitchBar() {
    if (names.isEmpty) return Container();

    return Container(
      margin: EdgeInsets.symmetric(vertical: 10.rpx),
      padding: EdgeInsets.only(right: 10.rpx),
      child: QGSwitchWidget(
          types:1,
          index: type,
          items: names,
          onSwitch: (index) {
            type = index;
            list = null;
            setState(() {});
            onRefresh();
          }),
    );
  }

  onSearch() {
    Navigator.of(context).pushNamed(PageNoticeSearch);
  }

  toDetail(index) {

     var args = {"title": getCateName(list[index]), "data": list[index]};
     Navigator.of(context).pushNamed(PageNoticeDetial, arguments: args);
  }
}
